<?php
include_only();
require_once "config.php";

/////////////////////////////////////////////////////////////////////
function getmicrotime()
{
    $res = explode(' ',microtime());
    return $res[1];
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function is_user_loggedin()
{
if(isset($_COOKIE["username"]) and isset($_COOKIE["password"])){
$username = $_COOKIE["username"];
$password = $_COOKIE["password"];

$result = mysql_query("SELECT * FROM `users` WHERE `username`='$username' and `password`='$password'");
$num_rows = mysql_num_rows($result);
if($num_rows==1)
{
$res = "true";
}
else
{
$res = "false";
}
}
else
{
$res = "false";
}
return $res;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_user_info()
{
$info = array();
if(isset($_COOKIE["username"]) and isset($_COOKIE["password"])){
$username = $_COOKIE["username"];
$password = $_COOKIE["password"];
$result = mysql_query("SELECT * FROM `users` WHERE `username`='$username' and `password`='$password'");
$row = mysql_fetch_array($result);

$info['email'] = $row['email'];
$info['id'] = $row['id'];
$info['username'] = $row['username'];
$info['user_group'] = $row['user_group'];
$info['password'] = $row['password'];
$info['confirmed'] = $row['confirmed'];
$info['retrive_key'] = $row['retrive_key'];
}
return $info;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_user_videos($uid)
{
$info = array();
$result = mysql_query("SELECT * FROM `videos` WHERE `userid`='$uid'");
$count = -1;
while($row = mysql_fetch_array($result)){
$count = $count + 1;
$info[$count]['id'] = $row['id'];
$info[$count]['userid'] = $row['userid'];
$info[$count]['url'] = $row['url'];
$info[$count]['name'] = $row['name'];
}
return $info;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_user_favourite_videos($uid)
{
$info = array();
$result = mysql_query("SELECT * FROM `favourite_videos` WHERE `userid`='$uid'");
$count = -1;
while($row = mysql_fetch_array($result)){
$count = $count + 1;
$info[$count]['id'] = $row['id'];
$info[$count]['userid'] = $row['userid'];
$info[$count]['url'] = $row['url'];
$info[$count]['name'] = $row['name'];
}
return $info;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_user_subtitles($user_id)
{
$info =array(); 
$result = mysql_query("SELECT * FROM `subtitles` WHERE `user_id`='$user_id'");
$count = -1;
while($row = mysql_fetch_array($result)){
$count = $count + 1;
$info[$count]['id'] = $row['id'];
$info[$count]['video_id'] = $row['video_id'];
$info[$count]['title'] = $row['title'];
$info[$count]['user_id'] = $row['user_id'];
$info[$count]['table_name'] = $row['table_name'];
$info[$count]['admin'] = $row['admin'];
$info[$count]['is_favourite_video'] = $row['is_favourite_video'];
}
return $info;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_video_information($video_id)
{
$result = mysql_query("SELECT * FROM `videos` WHERE `id`='$video_id'");
$count = -1;

$row = mysql_fetch_array($result);

$info['id'] = $row['id'];
$info['userid'] = $row['userid'];
$info['url'] = $row['url'];
$info['name'] = $row['name'];
$info['desc'] = $row['desc'];
$info['image0'] = $row['image0'];
$info['image1'] = $row['image1'];
$info['image2'] = $row['image2'];
$info['image3'] = $row['image3'];
$info['allow_favourites'] = $row['allow_favourites'];
$info['views'] = $row['views'];
return $info;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_favourite_video_information($video_id)
{
$result = mysql_query("SELECT * FROM `favourite_videos` WHERE `id`='$video_id'");
$count = -1;

$row = mysql_fetch_array($result);

$info['id'] = $row['id'];
$info['userid'] = $row['userid'];
$info['url'] = $row['url'];
$info['name'] = $row['name'];
$info['desc'] = $row['desc'];
$info['image0'] = $row['image0'];
$info['image1'] = $row['image1'];
$info['image2'] = $row['image2'];
$info['image3'] = $row['image3'];
return $info;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_subtitle_info($subtitle_id)
{
$result = mysql_query("SELECT * FROM `subtitles` WHERE `id` = '$subtitle_id' LIMIT 1");

$row = mysql_fetch_array($result);

$info['id'] = $row['id'];
$info['video_id'] = $row['video_id'];
$info['user_id'] = $row['user_id'];
$info['table_name'] = $row['table_name'];
$info['admin'] = $row['admin'];
$info['title'] = $row['title'];
$info['language'] = $row['language'];
$info['original_language'] = $row['original_language'];
$info['public'] = $row['public'];
$info['request'] = $row['request'];
$info['is_favourite_video'] = $row['is_favourite_video'];
$info['increase_replay_time'] = $row['increase_replay_time'];

return $info;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function arrange_subtitle_lines_order($table_name)
{
$result = mysql_query("SELECT * FROM `$table_name`");

$count = 0;

while ($row = mysql_fetch_array($result)) {
     $id = $row['id'];
     $start = $row['start'];
	 $start_to_number = str_replace(":", "", $start);
	 $count = $count + 1;
	 
mysql_query("UPDATE `$table_name` SET `start_to_number` = '$start_to_number' WHERE `id` =$id LIMIT 1 ");
}

$result = mysql_query("SELECT * FROM `$table_name` ORDER BY start ASC ");

$count = 0;

while ($row = mysql_fetch_array($result)) {
     $id = $row['id'];
     $line_number = $row['line_number'];
     $start = $row['start'];
     $end = $row['end'];
     $text = $row['text'];	 

	 $count = $count + 1;
	 
mysql_query("UPDATE `$table_name` SET `line_number` = '$count' WHERE `id` =$id LIMIT 1 ");
}

}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function convert_srt_time_to_seconds($srt_time)
{
$srt_time = explode(":", $srt_time);
$hours = $srt_time[0] * 60 * 60;
$minutes = $srt_time[1] * 60;
$seconds = $srt_time[2];
$result = $hours + $minutes + $seconds;
$result = explode(".", $result);
$result = $result[0];
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function convert_srt_time_to_seconds_full($srt_time)
{
$srt_time = explode(":", $srt_time);
$hours = $srt_time[0] * 60 * 60;
$minutes = $srt_time[1] * 60;
$seconds = $srt_time[2];
$result = $hours + $minutes + $seconds;
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function convert_seconds_to_srt_time($srt_time)
{
if($srt_time>=3600)
{
$hours = intval($srt_time / 3600);
if(strlen($hours)==1){$hours = '0'.$hours;}
$srt_time = $srt_time - ($hours * 3600);
}
else
{
$hours = '00';
}

if($srt_time<3600 and $srt_time>59)
{
$minutes = intval($srt_time / 60);
if(strlen($minutes)==1){$minutes = '0'.$minutes;}
$srt_time = $srt_time - ($minutes * 60);
}
else
{
$minutes = '00';
}

if($srt_time<=59)
{
$seconds = $srt_time;
}

$seconds_adjust = explode('.',$seconds);
//print_r($seconds_adjust);
if(strlen($seconds_adjust[0])==1){$seconds_adjust[0] = '0'.$seconds_adjust[0];}
if(count($seconds_adjust)==1){$seconds_adjust[1] = '000';}
if(strlen($seconds_adjust[1])==1){$seconds_adjust[1] = $seconds_adjust[1].'00';}
if(strlen($seconds_adjust[1])==2){$seconds_adjust[1] = $seconds_adjust[1].'0';}

$seconds = $seconds_adjust[0].".".$seconds_adjust[1];
$result = $hours.":".$minutes.":".$seconds;
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function export_srt_subtitle($table_name)
{
$srt_data = "";
$result = mysql_query("SELECT * FROM `$table_name` ORDER BY line_number ASC ");
while ($row = mysql_fetch_array($result)) {
     $id = $row['id'];
     $line_number = $row['line_number'];
     $start = $row['start'];
     $end = $row['end'];
     $text = $row['text'];	

	 $srt_data = $srt_data.$line_number."\n";
	 $srt_data = $srt_data.$start." --> ".$end."\n";
	 $srt_data = $srt_data.$text."\n\n";
	 }
	 
return $srt_data;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function export_player_srt_subtitle($table_name)
{
$srt_data = "<?xml version='1.0' encoding='UTF-8'?>
<tt xml:lang='en' xmlns='http://www.w3.org/2006/04/ttaf1'  xmlns:tts='http://www.w3.org/2006/04/ttaf1#styling'>
  <head>
   <styling>
      <style id='1' tts:textAlign='right'/>
      <style id='2' tts:color='transparent'/>
      <style id='3' style='2' tts:backgroundColor='white'/>
      <style id='4' style='2 3' tts:fontSize='20'/>
   </styling>
  </head>
  <body>
   <div xml:lang='en'>
";

$result = mysql_query("SELECT * FROM `$table_name` ORDER BY line_number ASC ");
while ($row = mysql_fetch_array($result)) {
     $id = $row['id'];
     $line_number = $row['line_number'];
     $start = $row['start'];
     $end = $row['end'];
     $text = $row['text'];	

	 $srt_data = $srt_data."<p begin='".$start."' end='".$end."'>".$text."</p>"."\n";
	 //$srt_data = $srt_data.$line_number;
	 //$srt_data = $srt_data.$start." --> ".$end."\n";
	 //$srt_data = $srt_data.$text."\n\n";
	 }
$srt_data = $srt_data . "</div>
  </body>
</tt>
";
	 
return $srt_data;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function sign_out()
{
setcookie('username','',time()-3600);
setcookie('password','',time()-3600);
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function load_content_template($template_name, $div_title, $div_content)
{
include "templates/".read_option('theme_folder')."/".$template_name;
$content = $table_content;
return $content;						
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function message_box($title, $content)
{

if(is_array($content))
{
$content_array = "<ul>";
for ($i = 0; $i <= count($content)-1; $i++) {
    $content_array = $content_array . "<li type='disc'>" . $content[$i] . "</li>";
}
$content_array = $content_array . "</ul>";
$content = $content_array ;
}

include "templates/".read_option('theme_folder')."/message_box.tbl";
$message = $message_box;
return $message;						
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function error_message($title, $content)
{

if(is_array($content))
{
$content_array = "<ul>";
for ($i = 0; $i <= count($content)-1; $i++) {
    $content_array = $content_array . "<li type='disc'>" . $content[$i] . "</li>";
}
$content_array = $content_array . "</ul>";
$content = $content_array ;
}

include "templates/".read_option('theme_folder')."/error_message.php";
$message = $error_message;
return $message;						
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function info_message($title, $content)
{

if(is_array($content))
{
$content_array = "<ul>";
for ($i = 0; $i <= count($content)-1; $i++) {
    $content_array = $content_array . "<li type='disc'>" . $content[$i] . "</li>";
}
$content_array = $content_array . "</ul>";
$content = $content_array ;
}

include "templates/".read_option('theme_folder')."/info_message.php";
$message = $info_message;
return $message;						
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function sign_in($username,$password)
{
if(preg_match("/^[a-zA-Z0-9]+$/", $username) != 1) {
    // string only contain the a to z , A to Z, 0 to 9
	$error[]="ERROR: user name can only contain numbers and letters.";
}

if(preg_match("/^[a-zA-Z0-9]+$/", $password) != 1) {
    // string only contain the a to z , A to Z, 0 to 9
	$error[]="ERROR: password can only contain numbers and letters.";
}

if(count($error)==0) {

$password = sha1($password);

$result = mysql_query("SELECT * FROM `users` WHERE `username`='$username' and `password`='$password'");
$num_rows = mysql_num_rows($result);
if($num_rows==1){
$expire=time()+60*60*24*30;
setcookie("username", $username, $expire);
setcookie("password", $password, $expire);
}
else
{
$error[]="invalid user name or password.";
}
}
if(count($error)>0)
{
return $error;
}
else
{
return "yes";
}
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function register($username,$password,$email)
{
if(preg_match("/^[a-zA-Z0-9]+$/", $username) != 1) {
    // string only contain the a to z , A to Z, 0 to 9
	$error_message[]="ERROR: user name can only contain numbers and letters";
}

if(preg_match("/^[a-zA-Z0-9]+$/", $password) != 1) {
    // string only contain the a to z , A to Z, 0 to 9
	$error_message[]="ERROR: password can only contain numbers and letters";
}

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
  //echo "Valid email address.";
}
else {
  //echo "Invalid email address.";
  $error_message[]="ERROR: invalid email address";
}

if(count($error)>0){
return $error_message;
}

 $key =  generate_random_string('50');
 
if(count($error)==0){
$password = sha1($password);
mysql_query("INSERT INTO `users` (`username` ,`password` ,`email` ,`user_group` ,`retrive_key`) VALUES ('$username', '$password', '$email', '3', '$key');");
return "yes";
}
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_video_file_and_table_name($subtitle_id)
{
$subtitle_ifo = get_subtitle_info($subtitle_id);
$subtitle_video_id = $subtitle_ifo['video_id']; 
$subtitle_table_name = $subtitle_ifo['table_name'];
if($subtitle_ifo['is_favourite_video']=='1'){$viddeo_info = get_favourite_video_information($subtitle_video_id);}else{$viddeo_info = get_video_information($subtitle_video_id);}
$subtitle_video_file_name = $viddeo_info['url'];

$result['video_file_name'] = $subtitle_video_file_name; 
$result['subtitle_table_name'] = $subtitle_table_name;

return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function generate_random_number($start, $end)
{
$random_number = rand($start, $end);
return $random_number;
} 
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function generate_random_string($length)
{
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $string = '';    

    for ($p = 0; $p < $length; $p++) 
	{
        $string .= $characters[mt_rand(0, strlen($characters))];
    }

    return $string;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_random_thumbnail($image_number)
{
$res = mysql_query("SELECT * FROM `videos` WHERE `image".$image_number."` <> '' order by rand() limit 1");
while ($row = mysql_fetch_array($res)) {
    $result['id']=$row['id']; 
    $result['userid']=$row['userid']; 
    $result['url']=$row['url']; 
    $result['name']=$row['name']; 
    $result['desc']=$row['desc']; 
    $result['image0']=$row['image0']; 
    $result['image1']=$row['image1']; 
    $result['image2']=$row['image2']; 
    $result['image3']=$row['image3']; 	
}
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_subtitle_progress($table_name)
{
$result = mysql_query("SELECT * FROM `$table_name`");
$total = mysql_num_rows($result);
$result = mysql_query("SELECT * FROM `$table_name` WHERE `done` = '1'");
$done = mysql_num_rows($result);
$res = ($done * 100) / $total ;
$res= floor($res);
return $res;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_subtitles_requests()
{
$res = mysql_query("SELECT * FROM `subtitles` WHERE `request` = '1'");
$count=-1;
while ($row = mysql_fetch_array($res)) {

$count = $count + 1 ;	

    $result[$count]['id']=$row['id']; 
    $result[$count]['video_id']=$row['video_id']; 
    $result[$count]['user_id']=$row['user_id']; 
	$result[$count]['table_name']=$row['table_name']; 
	$result[$count]['admin']=$row['admin'];
	$result[$count]['title']=$row['title'];
	$result[$count]['language']=$row['language'];
	$result[$count]['public']=$row['public'];
	$result[$count]['request']=$row['request'];	
	
}
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_random_open_subtitles($count)
{
$res = mysql_query("SELECT * FROM `subtitles` where `video_id` <> '-1' ORDER BY rand() LIMIT $count ");
$count=-1;
while ($row = mysql_fetch_array($res)) {

$count = $count + 1 ;	

    $result[$count]['id']=$row['id']; 
    $result[$count]['video_id']=$row['video_id']; 
    $result[$count]['user_id']=$row['user_id']; 
	$result[$count]['table_name']=$row['table_name']; 
	$result[$count]['admin']=$row['admin'];
	$result[$count]['title']=$row['title'];
	$result[$count]['language']=$row['language'];
	$result[$count]['public']=$row['public'];
	$result[$count]['request']=$row['request'];	
	$result[$count]['is_favourite_video']=$row['is_favourite_video'];		
}
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_random_finished_subtitles($count)
{
$res = mysql_query("SELECT * FROM `subtitles` WHERE 1 ORDER BY rand() LIMIT $count ");
$count=-1;
while ($row = mysql_fetch_array($res)) {

$count = $count + 1 ;	

    $result[$count]['id']=$row['id']; 
    $result[$count]['video_id']=$row['video_id']; 
    $result[$count]['user_id']=$row['user_id']; 
	$result[$count]['table_name']=$row['table_name']; 
	$result[$count]['admin']=$row['admin'];
	$result[$count]['title']=$row['title'];
	$result[$count]['language']=$row['language'];
	$result[$count]['public']=$row['public'];
	$result[$count]['request']=$row['request'];	
	$result[$count]['is_favourite_video']=$row['is_favourite_video'];		
}
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_random_videos($count)
{
$res = mysql_query("SELECT * FROM `videos` WHERE 1 ORDER BY rand() LIMIT $count ");
$count=-1;
while ($row = mysql_fetch_array($res)) {

$count = $count + 1 ;	

    $result[$count]['id']=$row['id']; 
    $result[$count]['userid']=$row['userid']; 
    $result[$count]['url']=$row['url']; 
	$result[$count]['name']=$row['name']; 
	$result[$count]['desc']=$row['desc'];
	$result[$count]['image0']=$row['image0'];
	$result[$count]['image1']=$row['image1'];
	$result[$count]['image2']=$row['image2'];
	$result[$count]['image3']=$row['image3'];	
	$result[$count]['allow_favourites']=$row['allow_favourites'];	
	$result[$count]['views']=$row['views'];		
}
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_team_info($subtitle_id)
{
$result = array();
$res = mysql_query("SELECT * FROM `teams` WHERE `subtitle_id` = '$subtitle_id'");
$count=-1;
while ($row = mysql_fetch_array($res)) {

$count = $count + 1 ;	

    $result[$count]['id']=$row['id']; 
    $result[$count]['subtitle_id']=$row['subtitle_id']; 
    $result[$count]['user_id']=$row['user_id']; 	
	$result[$count]['state']=$row['state']; 
}
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_accepted_team_members($subtitle_id)
{
$result = array();
$res = mysql_query("SELECT * FROM `teams` WHERE `subtitle_id` = '$subtitle_id' and `state` = '1'");
$count=-1;
while ($row = mysql_fetch_array($res)) {

$count = $count + 1 ;	

    $result[$count]['id']=$row['id']; 
    $result[$count]['subtitle_id']=$row['subtitle_id']; 
    $result[$count]['user_id']=$row['user_id']; 	
	$result[$count]['state']=$row['state']; 
}
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_all_languages()
{
$res = mysql_query("SELECT * FROM `language` WHERE 1");
$count = -1;
while ($row = mysql_fetch_array($res)) {
$count = $count + 1;
    $result[$count]['id']=$row['id']; 
    $result[$count]['name']=$row['name']; 
    $result[$count]['lang']=$row['lang'];	
}
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_language_by_id($id)
{
$res = mysql_query("SELECT * FROM `language` WHERE `id` = '$id'");
while ($row = mysql_fetch_array($res)) {
    $result['id']=$row['id']; 
    $result['name']=$row['name']; 	
    $result['lang']=$row['lang']; 		
}
if(empty($result))
{
    $result['id']=''; 
    $result['name']=''; 	
    $result['lang']=''; 	
}
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function export_subtitle($subtitle_id)
{
$subtitle_info = get_subtitle_info($subtitle_id);
//print_r($subtitle_info);
$subtitle_tabel_name = $subtitle_info['table_name'];
//echo $subtitle_tabel_name;
$srt = "";
$res = mysql_query("SELECT * FROM `$subtitle_tabel_name`");
while ($row = mysql_fetch_array($res)) {
    $srt = $srt . $row['line_number'] . PHP_EOL; 
    $srt = $srt . str_replace(".", ",", $row['start']) . " --> " . str_replace(".", ",", $row['end']) . PHP_EOL; 	
    $srt = $srt . br2nl($row['text']) . PHP_EOL; 
    $srt = $srt . PHP_EOL; 	
}
return $srt;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
FUNCTION br2nl($string){
RETURN PREG_REPLACE('#<br\s*?/?>#i', PHP_EOL, $string); 
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function include_only()
{
if(basename(__FILE__) == basename($_SERVER['PHP_SELF']))
{
echo "restricted access.";
exit();
}
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_user_name()
{
$user_info = get_user_info();
$user_name = $user_info['username'];
return $user_name;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_user_group()
{
$user_info = get_user_info();
$user_group = $user_info['user_group'];
return $user_group;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_member_info($user_id)
{
$result = array();
$res = mysql_query("SELECT * FROM `users` WHERE `id` = '$user_id'");
$row = mysql_fetch_array($res);
$result['username'] = $row['username'];
$result['email'] = $row['email'];
$result['id'] = $row['id'];
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function read_option($option_name)
{
$res = mysql_query("SELECT * FROM `options` WHERE `option_name` = '$option_name'");
$row = mysql_fetch_array($res);
$option_value = $row['option_value'];
return $option_value;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function write_option($option_name, $option_value)
{
$res = mysql_query("UPDATE `options` SET `option_value` = '$option_value' WHERE `option_name` = '$option_name' LIMIT 1 ;");
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function read_module_option($module_name, $option_name)
{
$res = mysql_query("SELECT * FROM `modules` WHERE `module_name` = '$module_name' and `option_name` = '$option_name'")/*or die("Query failed with error: ".mysql_error())*/;
$row = mysql_fetch_array($res);
$option_value = $row['value'];
return $option_value;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function write_module_option($module_name, $option_name, $value)
{
$res = mysql_query("UPDATE `modules` SET `value` = '$value' WHERE `module_name` = '$module_name' and `option_name` = '$option_name' LIMIT 1 ;");
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_users_count()
{
$res = mysql_query("SELECT * FROM `users`");
$users_count = mysql_num_rows($res);
return $users_count;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_subtitles_count()
{
$res = mysql_query("SELECT * FROM `subtitles`");
$subtitles_count = mysql_num_rows($res);
return $subtitles_count;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_videos_count()
{
$res = mysql_query("SELECT * FROM `videos`");
$videos_count = mysql_num_rows($res);
return $videos_count;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_all_videos()
{
$res = mysql_query("SELECT * FROM `videos` ");
$count= -1;
while ($row = mysql_fetch_array($res)) {
$count = $count + 1 ;
    $result[$count]['id']=$row['id']; 
    $result[$count]['userid']=$row['userid']; 	
    $result[$count]['url']=$row['url']; 		
    $result[$count]['name']=$row['name'];
	$result[$count]['desc']=$row['desc'];
	$result[$count]['image0']=$row['image0'];
	$result[$count]['image1']=$row['image1'];
	$result[$count]['image2']=$row['image2'];
	$result[$count]['unage3']=$row['image3'];	
}
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_all_subtitles()
{
$res = mysql_query("SELECT * FROM `subtitles` ");
$count= -1;
while ($row = mysql_fetch_array($res)) {
$count = $count + 1 ;
    $result[$count]['id']=$row['id']; 
    $result[$count]['video_id']=$row['video_id']; 	
    $result[$count]['user_id']=$row['user_id']; 		
    $result[$count]['table_name']=$row['table_name'];
	$result[$count]['admin']=$row['admin'];
	$result[$count]['title']=$row['title'];
	$result[$count]['language']=$row['language'];
	$result[$count]['public']=$row['public'];
	$result[$count]['request']=$row['request'];	
	$result[$count]['original_language']=$row['original_language'];	
}
return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_user_team_subtitles($user_id)
{
$result = mysql_query("SELECT * FROM `teams` WHERE `user_id`='$user_id' and `state`='1'");
$count = -1;
while($row = mysql_fetch_array($result)){
$count = $count + 1;
$subtitles[$count]['id'] = $row['id'];
$subtitles[$count]['subtitle_id'] = $row['subtitle_id'];
$subtitles[$count]['user_id'] = $row['user_id'];
$subtitles[$count]['state'] = $row['state'];
}

for ($i = 0; $i <= count($subtitles)-1; $i++) {
    $info[$i] = get_subtitle_info($subtitles[$i]['subtitle_id']);
}

return $info;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function search_subtitles($text)
{
$text = trim($text);
$result = mysql_query("SELECT * FROM `subtitles` WHERE `title` like '%$text%' ");
$count = -1;
while($row = mysql_fetch_array($result)){
$count = $count + 1;
$info[$count]['id'] = $row['id'];
$info[$count]['video_id'] = $row['video_id'];
$info[$count]['user_id'] = $row['user_id'];
$info[$count]['table_name'] = $row['table_name'];
$info[$count]['admin'] = $row['admin'];
$info[$count]['title'] = $row['title'];
$info[$count]['language'] = $row['language'];
$info[$count]['public'] = $row['public'];
$info[$count]['request'] = $row['request'];
$info[$count]['original_language'] = $row['original_language'];
}
return $info;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function search_videos($text)
{
$text = trim($text);
$result = mysql_query("SELECT * FROM `videos` WHERE `name` like '%$text%' ");
$count = -1;
while($row = mysql_fetch_array($result)){
$count = $count + 1;
$info[$count]['id'] = $row['id'];
$info[$count]['userid'] = $row['userid'];
$info[$count]['url'] = $row['url'];
$info[$count]['name'] = $row['name'];
$info[$count]['desc'] = $row['desc'];
$info[$count]['image0'] = $row['image0'];
$info[$count]['image1'] = $row['image1'];
$info[$count]['image2'] = $row['image2'];
$info[$count]['image3'] = $row['image3'];
}
return $info;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_subtitle_lines_count($table_name)
{
$res = mysql_query("SELECT * FROM `$table_name`");
$subtitle_lines_count = mysql_num_rows($res);
return $subtitle_lines_count;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function is_valid_subtitle_page_index($table_name, $line_number)
{
$res = mysql_query("SELECT * FROM `$table_name` WHERE `line_number`='$line_number' LIMIT 1");
$lines_count = mysql_num_rows($res);
if($lines_count>0){return "true";}else{return "false";}
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function is_subtitle_empty($table_name)
{
if(get_subtitle_lines_count($table_name)<1)
{
mysql_query("INSERT INTO `$table_name` (`line_number` ,`start` ,`end` ,`text`) VALUES ('1', '00:00:00.000', '00:00:00.001', 'EMPTY LINE');");
}
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function create_subtitle_table($table_name)
{
mysql_query("CREATE TABLE `$table_name` (
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`line_number` BIGINT NOT NULL ,
`start` TEXT NOT NULL ,
`end` TEXT NOT NULL ,
`text` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`translated_text` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,
`done` INT NOT NULL ,
`start_to_number` BIGINT NOT NULL
) ENGINE = MYISAM ;");
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function add_video_to_favourites($video_id, $userid)
{
$fav_video_info = get_video_information($video_id);

$url = $fav_video_info['url'];
$name = $fav_video_info['name'];
$desc = $fav_video_info['desc'];
$image0 = $fav_video_info['image0'];
$image1 = $fav_video_info['image1'];
$image2 = $fav_video_info['image2'];
$image3 = $fav_video_info['image3'];

mysql_query("INSERT INTO `favourite_videos` (`userid` ,`url` ,`name` ,`desc` ,`image0` ,`image1` ,`image2` ,`image3`) VALUES ('$userid', '$url', '$name', '$desc', '$image0', '$image1', '$image2', '$image3');");
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function write_video_comment($video_id, $userid, $message)
{
$current_time = getmicrotime();
mysql_query("INSERT INTO `videos_comments` (`userid` ,`message` ,`time` ,`video_id`) VALUES ('$userid', '$message', '$current_time', '$video_id');");
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function check_comment_duplicate($video_id, $userid, $message)
{
$res = mysql_query("SELECT * FROM `videos_comments` WHERE `userid`='$userid' and `video_id`='$video_id' and `message`='$message'");
$res_count = mysql_num_rows($res);
if($res_count>0){return 'yes';}else{return 'no';}
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function read_video_comments($video_id)
{
$comments = array();
$result = mysql_query("SELECT * FROM `videos_comments` WHERE `video_id`='$video_id'");
$count = -1;
while($row = mysql_fetch_array($result)){
$count = $count + 1;
$comments[$count]['id'] = $row['id'];
$comments[$count]['userid'] = $row['userid'];
$comments[$count]['message'] = $row['message'];
$comments[$count]['time'] = $row['time'];
$comments[$count]['video_id'] = $row['video_id'];
}
return $comments;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function add_to_maillist($email)
{
$random = generate_random_number('9999999999999999999999','999999999999999999999');
mysql_query("INSERT INTO `online_subtitles`.`maillist` (
`email` ,
`key`
)
VALUES (
'$email', '2'
);
");
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function can_edit_subtitle($user_id, $subtitle_id)
{
$can_edit = "false";
$user_subtitles = get_user_subtitles($user_id);

for ($i = 0; $i <= count($user_subtitles)-1; $i++) {
    if($subtitle_id==$user_subtitles[$i]['id']){$can_edit="true";}
}

$subtitle_team = get_team_info($subtitle_id);
for ($i = 0; $i <= count($subtitle_team)-1; $i++) {
    if($user_id==$subtitle_team[$i]['user_id'] and $subtitle_team[$i]['state']=='1')
	{$can_edit="true";}
}

return $can_edit;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function is_subtitle_owner($user_id, $subtitle_id)
{
$subtitle_owner = "false";
$user_subtitles = get_user_subtitles($user_id);

for ($i = 0; $i <= count($user_subtitles)-1; $i++) {
    if($subtitle_id==$user_subtitles[$i]['id']){$subtitle_owner="true";}
}

return $subtitle_owner;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function secure_text($text)
{
$text = mysql_real_escape_string($text);
return $text;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function unsecure_text($text)
{
$text = stripslashes($text);
return $text;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function validate_email($email)
{
 if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
  //echo "Valid email address.";
  return 'yes';
}
else {
  //echo "Invalid email address.";
  return 'no';
}
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function gen_user_key($user_id)
{
$key = generate_random_string('50');
mysql_query("UPDATE `users` SET `retrive_key` = '$key' WHERE `users`.`id` = '$user_id' LIMIT 1 ;");
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_user_info_by_email($email)
{
$res = mysql_query("SELECT * FROM `users` WHERE `email` = '$email' limit 1");
$row = mysql_fetch_array($res);

$result['id'] = $row['id'];
$result['username'] = $row['username'];
$result['email'] = $row['email'];
$result['confirmed'] = $row['confirmed'];
$result['retrive_key'] = $row['retrive_key'];
$result['user_group'] = $row['user_group'];

return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_user_info_by_username($username)
{
$res = mysql_query("SELECT * FROM `users` WHERE `username` = '$username' limit 1");
$row = mysql_fetch_array($res);

$result['id'] = $row['id'];
$result['username'] = $row['username'];
$result['email'] = $row['email'];
$result['confirmed'] = $row['confirmed'];
$result['retrive_key'] = $row['retrive_key'];
$result['user_group'] = $row['user_group'];

return $result;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_user_requests($user_id)
{
$requests = array(); 
$res = mysql_query("SELECT * FROM `teams` WHERE `user_id` = '$user_id'");
$row = mysql_fetch_array($res);

$count = -1;
while($row = mysql_fetch_array($res)){
$count = $count + 1;
$requests[$count]['id'] = $row['id'];
$requests[$count]['subtitle_id'] = $row['subtitle_id'];
$requests[$count]['user_id'] = $row['user_id'];
$requests[$count]['state'] = $row['state'];
}
return $requests;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function view_array($array)
{
echo "<pre>";
print_r($array);
echo "</pre>";
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function delete_subtitle($subtitle_id)
{
$subtitle_information = get_subtitle_info($subtitle_id);
$table_name = $subtitle_information['table_name'];
//echo $table_name;
mysql_query("DROP TABLE $table_name");
mysql_query("DELETE FROM `subtitles` WHERE `id` = '$subtitle_id' LIMIT 1");
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_subtitle_increase_replay_time($subtitle_id)
{
$subtitle_info = get_subtitle_info($subtitle_id);
$ipt = $subtitle_info['increase_replay_time'];
return $ipt;
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function set_subtitle_increase_replay_time($subtitle_id, $iseconds)
{
mysql_query("UPDATE `subtitles` SET `increase_replay_time` = '$iseconds' WHERE `id` ='$subtitle_id';");
}
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
function get_subtitle_lines_numbers($subtitle_id)
{
$subtitle_info = get_subtitle_info($subtitle_id);
$subtitle_table_name = $subtitle_info['table_name'];
$res = mysql_query("SELECT * FROM `$subtitle_table_name` ORDER by line_number ASC");
$count = -1;
while($row = mysql_fetch_array($res)){
$count = $count + 1;
$lines[$count]['line_number'] = $row['line_number'];
}
return $lines;
}
/////////////////////////////////////////////////////////////////////
?>